<?php


use Doraemon\pockets\constInterface\FormatScene;
use Doraemon\service\HomePage\Schedule;
use Doraemon\service\Format\FormatMix;
use Doraemon\service\statSign\Basic as basicStatSign;
use Doraemon\pockets\constInterface\statSign\AutoModule;
use Doraemon\tools\BlockType;
use Doraemon\tools\HhzClient;
use Doraemon\pockets\IdSender\IdSender;
use Doraemon\tools\Link;

class DynamicListAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;

    /**
     * @throws Exception
     */
    public function main()
    {
        $store_id = $this->getRequest()->getPost('store_id');
        $page = $this->getRequest()->getPost('page');
        if(empty($store_id)){
                throw new \Exception("门店id缺失", self::CODE_ERROR_VIEW_TOSTA);
        }
        $store_uid = \Biz\service\Store\StoreBasicService::getStoreUidById($store_id);
        if(empty($store_uid)){
            throw new \Exception("门店uid缺失", self::CODE_ERROR_VIEW_TOSTA);
        }
        $sType = Schedule::getType();
        $esList = Schedule::getHomepageList($page, $store_uid, $sType , 'addtime');
        $isOver = 1;
        $dynamic_list = [];
        if ($esList && $esList['total'] > 0 && !empty($esList['data'])) {
            $isOver = $esList['isOver'];
            $ObjBase = [];
            $case_ids = \Biz\service\Store\StoreHomeService::getObjIds($store_id) ? : [];
            $obj_ids = \Biz\service\Store\StoreContent::delContentList($store_id) ? : [];

            $obj_ids = array_merge($case_ids  , $obj_ids);
            foreach ($esList['data'] as $objId) {
                if(in_array($objId , $obj_ids)) continue;
                $objType = \Doraemon\tools\IdParse::getType($objId);
                $ObjBase[] = [
                    'obj_id' => $objId,
                    'obj_type' => $objType
                    ];
            }
            if($ObjBase && is_array($ObjBase)){
                $list =  FormatMix::format($ObjBase , $this->uid , FormatScene::POS_RECOMMEND_LINE);
                foreach ($list as $key => $value){
                    switch ($key){
                        case IdSender::TYPE_PHOTO:
                        case IdSender::TYPE_ANSWER:
                            foreach($value as $id => $info){
                                $photoData['photo_info']['show_type'] = $info['photo']['photo_info']['show_type'];
                                $photoData['counter'] = $info['photo']['counter'];
                                $list[$key][$id]['right_top'] = Link::iconFormat($photoData, Link::ICON_PRIVATE_NOTE);
                            }
                            break;
                        case IdSender::TYPE_ARTICLE:
                            foreach($value as $id => $info){
                                $artData['counter'] = $info['article']['counter'];
                                $list[$key][$id]['right_top'] = Link::iconFormat($artData, Link::ICON_PRIVATE_ARTICLE);
                            }
                            break;
                        case IdSender::TYPE_BLANK:
                            foreach($value as $id => $info){
                                $blankData['counter'] = $info['blank']['counter'];
                                $list[$key][$id]['right_top'] = Link::iconFormat($blankData, Link::ICON_PRIVATE_BLANK);
                            }
                            break;
                    }
                }
                foreach ($ObjBase as  $value){
                    //小程序过滤视频
                    if(HhzClient::getClientType() == HhzClient::APPTYPE_STORE_MINIPROGRAM){
//                        if($list[$value['obj_type']][$value['obj_id']]['photo']['photo_info']['video_info']) continue;
                    }
                    //小程序过滤视频
                    if(empty($list[$value['obj_type']][$value['obj_id']])) continue;
                    $list[$value['obj_type']][$value['obj_id']]['statSign'] = basicStatSign::genStatSign(AutoModule::MODULE_STORE_HOME_DYNAMIC_LIST, [
                        basicStatSign::EXTEND_KEY_OBJ_TYPE => $value['obj_type'],
                        basicStatSign::EXTEND_KEY_OBJ_ID => $value['obj_id'],
                    ], ['obj_id' => $value['obj_id'], 'obj_type' => $value['obj_type'], 'store_id' => $store_id]);
                    $dynamic_list[] = $list[$value['obj_type']][$value['obj_id']];
                }
            }
        }

        $data['dynamic_list'] = $dynamic_list ? : [];
        $data['is_over'] = $isOver ;
        return $this->setSuccess($data);
    }


}